package com.j2ee.servlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author JiangQikun
 * @date 2024/6/1 1:48
 */

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 设置获取数据的编码，用于显示中文
        req.setCharacterEncoding("UTF-8");

        // 从浏览器获取数据
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        System.out.println("name:" + name);
        System.out.println("password:" + password);

        // 返回数据给浏览器
        String html;
        if ("admin".equals(name) && "123".equals(password)) {
            html = "<div style='color:green'>success</div>";
        } else {
            html = "<div style='color:red'>fail</div>";
        }
        PrintWriter pw = resp.getWriter();
        resp.setContentType("text/html; charset=UTF-8");
        pw.println(html);



    }
}
